package com.felix.forum.web.admin;

import com.felix.forum.po.Article;
import com.felix.forum.po.User;
import com.felix.forum.service.ArticleService;
import com.felix.forum.service.TagService;
import com.felix.forum.service.TypeService;
import com.felix.forum.vo.ArticleQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * @author Administrator
 * @date 2022/02/16 16:43
 **/
@Controller
@RequestMapping("/admin")
public class ArticleController {

    private static final String INPUT = "admin/articleInput";
    private static final String LIST = "admin/articleManage";
    private static final String REDIRECT_LIST = "redirect:admin/articleManage";

    @Autowired
    private ArticleService articleService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @GetMapping("/articleManage")
    public String articleManage(@PageableDefault(size = 8, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                                ArticleQuery article, Model model) {
        model.addAttribute("types",typeService.listType());
        model.addAttribute("page", articleService.listArticle(pageable, article));
        return "admin/articleManage";
    }

    @PostMapping("/articleManage/search")
    public String search(@PageableDefault(size = 8, sort = {"updateTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                         ArticleQuery article, Model model) {
        model.addAttribute("page", articleService.listArticle(pageable, article));
        return "admin/articleManage :: articleList";
    }

    private void setTypeAndTag(Model model){
        model.addAttribute("types",typeService.listType());
        model.addAttribute("tags", tagService.listTag());
    }

    @GetMapping("/articleManage/input")
    public String input(Model model) {
        setTypeAndTag(model);
        model.addAttribute("article", new Article());
        return "admin/articleInput";
    }

    @GetMapping("/articleManage/{id}/input")
    public String editInput(@PathVariable Long id, Model model) {
        setTypeAndTag(model);
        Article article = articleService.getArticle(id);
        article.init();
        model.addAttribute("article", articleService.getArticle(id));
        return "admin/articleInput";
    }

    @PostMapping("/articleManage")
    public String post(Article article, RedirectAttributes attributes, HttpSession session) {
        article.setUser((User) session.getAttribute("user"));
        article.setType(typeService.getType(article.getType().getId()));
        article.setTags(tagService.listTag(article.getTagIds()));
        Article article1;
        if (article.getId() == null) {
            article1 = articleService.saveArticle(article);
        } else {
            article1 = articleService.updateArticle(article.getId(), article);
        }
        if (article1 == null) {
            attributes.addFlashAttribute("message", "操作失败！");
        } else {
            attributes.addFlashAttribute("message", "操作成功！");
        }
        return "redirect:/admin/articleManage";
    }

    @GetMapping("/articleManage/{id}/delete")
    public String delete(@PathVariable Long id, RedirectAttributes attributes) {
        articleService.deleteArticle(id);
        attributes.addFlashAttribute("message", "删除成功！");
        return "redirect:/admin/articleManage";
    }
}
